package demo3;

import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-08-09
 * Time: 10:45
 */
public class TestDemo2 {

    /**
     * 进行普通文件的复制
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("请输入你要复制的源文件路径");
        Scanner scanner = new Scanner(System.in);
        String s1 = scanner.next();
        File srcFile = new File(s1);
        if(!srcFile.isFile()){
            System.out.println("该文件不是普通的文件~~~");
            return;
        }
        if(!srcFile.exists()){
            System.out.println("该文件不存在~~~");
            return;
        }
        System.out.println("请输入复制的目标路径~~~");
        Scanner scan2 = new Scanner(System.in);
        String s2 = scan2.next();
        File destFile = new File(s2);
        if(destFile.exists()){
            System.out.println("该文件已经存在~~~");
            return ;
        }
        //开始进行复制--读取源文件--复制到目标文件路径
        try(InputStream inputStream = new FileInputStream(srcFile)){
            try(OutputStream outputStream = new FileOutputStream(destFile)){
                byte[] bytes = new byte[1024];
                while(true){
                    int len = inputStream.read(bytes);
                    if(len==-1){
                        break;
                    }
                    outputStream.write(bytes);
                }
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }

}
